#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<cassert>
using namespace std;

void Resolve(const string& expr)
{
	vector<char> vecLetter;
	for (auto c : expr)
	{
		if (isalpha(c))
		{
			vecLetter.push_back(c);
		}
	}

	sort(vecLetter.begin(), vecLetter.end());
	vecLetter.erase(unique(vecLetter.begin(), vecLetter.end()), vecLetter.end());
	
	assert(vecLetter.size() <= 10);

	int idata[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	while (next_permutation(idata, idata + 10))
	{
		stack<int> stResult;
		int curValue = 0;
		char lastOpc = (char)0;

		for (auto c : expr)
		{
			if (isalpha(c))
			{
				int idx = distance(vecLetter.begin(), find(vecLetter.begin(), vecLetter.end(), c));
				curValue = curValue * 10 + idata[idx];
			}
			else
			{
				if (
					(lastOpc == (char)0)	//this is the first opc
					|| (((c == '*') || (c == '/')) && ((lastOpc == '+') || (lastOpc == '-')))	//this opc is higher than last opc
					)
				{
					stResult.push(curValue);
				}
				else
				{
					//this is not the first opc, and the opc is not higher than another
					//so we can calc last expression
					int opr2 = curValue;
					char opc = (char)stResult.top();
					stResult.pop();
					int opr1 = stResult.top();
					stResult.pop();

					switch (opc)
					{
					case '+':
						stResult.push(opr1 + opr2);
						break;
					case '-':
						stResult.push(opr1 - opr2);
						break;
					case '*':
						stResult.push(opr1 * opr2);
						break;
					case '/':
						stResult.push(opr1 / opr2);
						break;
					default:
						assert(false);
						break;
					}
				}

				curValue = 0;
				lastOpc = c;
				stResult.push((int)c);
			}
		}

		int opr2 = curValue;
		char opc = (char)stResult.top();
		stResult.pop();
		int opr1 = stResult.top();
		stResult.pop();
		assert(stResult.empty());
		assert(opc == '=');

		if (opr1 == opr2)
		{
			for (auto c : expr)
			{
				if (isalpha(c))
				{
					int idx = distance(vecLetter.begin(), find(vecLetter.begin(), vecLetter.end(), c));
					cout << idata[idx];
				}
				else
				{
					cout << c;
				}
			}

			cout << endl;
		}
	}
}

int main(void)
{
	Resolve("READ+WRITE+TALK=SKILL");
	Resolve("We*love=CodeIQ");
}
